package com.cloudkinto.controller.detective;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dto.stock.defective.SkuStockTotalDto;
import com.cloudkinto.service.detective.DefectiveService;
import com.cloudkinto.service.detective.vo.StartPickReq;
import com.cloudkinto.service.detective.vo.pick.BatchReq;
import com.cloudkinto.service.stock.ProductStockService;
import com.cloudkinto.vo.defective.DefectiveAddReq;
import com.cloudkinto.vo.defective.DefectiveDetailRes;
import com.cloudkinto.vo.defective.DefectivePageDto;
import com.cloudkinto.vo.defective.DefectiveUpdateReq;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-12-11
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "次品处理Oms模块")
@RequestMapping("/api/admin/oms/Defective")
public class DefectiveOmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(DefectiveOmsController.class);

    private final DefectiveService service;
    private final ProductStockService productStockService;

    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated DefectiveAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功", service.add(req, userId, getCompanyId(), getTenantId()));
    }


    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated DefectiveUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId, getCompanyId(), getTenantId()));
    }


    @UserSessionRequired
    @PutMapping("submit")
    @ApiOperation(value = "提交", notes = "提交")
    public SingleResult submit(@RequestBody @Validated BatchReq req) {
        Long userId = getCurrentUserId();
        service.submit(req, userId, getCompanyId(), getTenantId());
        return SingleResult.success();
    }



    @UserSessionRequired
    @PutMapping("/cancel")
    @ApiOperation(value = "取消", notes = "取消")
    public SingleResult cancel(@RequestBody @Validated BatchReq req) {
        Long userId = getCurrentUserId();
        service.cancel(req, userId, getCompanyId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = DefectiveDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        map.put("companyId", getCompanyId());
        map.put("source", "oms");
        map.put("id", id);
        return SingleResult.success("查询成功", service.detail(map));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getTenantId(), "oms");
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    public SingleResult list(HttpServletRequest request) {
        Map<String, Object> map = this.getMap(request);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "stockKind", value = "处理库存类型 1FNSKU(暂时不做) 2 SKU"),
            @ApiImplicitParam(name = "storageIds", value = "仓库", type = "arr"),
            @ApiImplicitParam(name = "handleMethods", value = "处理类型(多选)", type = "arr"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time submit_time 等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "defectiveNo", value = "单号"),
            @ApiImplicitParam(name = "sku", value = "sku"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = DefectivePageDto.class))
    public PageResult listPage(HttpServletRequest request) {

        Map<String, Object> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    @UserSessionRequired
    @GetMapping("/statusStatistics")
    @ApiOperation(value = "状态数量统计", notes = "状态数量统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "stockKind", value = "处理库存类型 1FNSKU(暂时不做) 2 SKU"),
            @ApiImplicitParam(name = "storageIds", value = "仓库", type = "arr"),
            @ApiImplicitParam(name = "handleMethods", value = "处理类型(多选)", type = "arr"),
            @ApiImplicitParam(name = "dateColumn", value = "筛选日期类型 create_time submit_time 等等"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "defectiveNo", value = "单号"),
            @ApiImplicitParam(name = "sku", value = "sku"),
    })
    public SingleResult statusStatistics(HttpServletRequest request) {
        Map<String, Object> map = this.getMap(request);
        return SingleResult.success(service.statusStatistics(map));
    }


    public Map<String, Object> getMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String stockKind = request.getParameter("stockKind");
        String storageIds = request.getParameter("storageIds");
        String handleMethods = request.getParameter("handleMethods");
        String dateColumn = request.getParameter("dateColumn");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        String defectiveNo = request.getParameter("defectiveNo");
        String sku = request.getParameter("sku");
        String status = request.getParameter("status");
        map.put("stockKind", stockKind);
        if (storageIds != null) {
            map.put("storageIds", Arrays.asList(storageIds.split(",")));
        }
        if (handleMethods != null) {
            map.put("handleMethods", Arrays.asList(handleMethods.split(",")));
        }
        map.put("dateColumn", dateColumn);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("defectiveNo", defectiveNo);
        map.put("sku", sku);
        map.put("status", status);
        map.put("tenantId", getTenantId());
        map.put("companyId", getCompanyId());
        map.put("source", "oms");
        return map;
    }


    @UserSessionRequired
    @GetMapping("/getSkuStockTotalList")
    @ApiOperation(value = "新增编辑-添加产品按钮（分页）", notes = "新增编辑-添加产品按钮（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "sku/产品名称"),
            @ApiImplicitParam(name = "whId", value = "仓库id"),

            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = SkuStockTotalDto.class))
    public PageResult getSkuStockTotalList(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", getTenantId());
        map.put("source", "oms");
        map.put("companyId", getCompanyId());
        String keyword = request.getParameter("keyword");
        String whId = request.getParameter("whId");
        map.put("whId", whId);

        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        return productStockService.getSkuStockTotalList(map);
    }


}

